<template>
  <el-dialog title="新增下级字典" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item style="display:none">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input v-model="form.value"></el-input>
      </el-form-item>
      <el-form-item label="字典说明" prop="note">
        <el-input v-model="form.note"></el-input>
      </el-form-item>
      <!-- <el-form-item label="单位简称" prop="dwjc">
        <el-input v-model="form.dwjc"></el-input>
      </el-form-item>
      <el-form-item label="行政简称" prop="xzjc">
        <el-input v-model="form.xzjc"></el-input>
      </el-form-item>
       <el-form-item label="备注" prop="description"> 
        <el-input type="textarea" v-model="form.description"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit('addForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { baseUrl } from "@/config/env";
const urls = {
  // checkField: baseUrl + "/backletter/common/check_unique", // 验证编码唯一性
  save: baseUrl + "/dict/sys-dict/add", // 保存
  get: baseUrl + "/dict/sys-dict/edit_form"
};
export default {
  props : ['code'],
  data() {
    return {
      dialogVisible: false,
      super_depart: {
        value: "",
        note: "",
        objectId : null
      },
      form: {
        value: null,
        note: null,
        parentId : null,
        code : null
      },
      rules: {
        value: [{ required: true, message: "请输入字典值", trigger: "blur" }],
        code: [
          { required: true, message: "请输入部门编码", trigger: "change" },
          { min: 3, max: 20, message: "长度必须为3-20个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    show(typeCode,objectId,code) {
      this.dialogVisible = true;
      this.form.code = code
      this.form.parentId = objectId;
      if (this.$refs["addForm"]) {
        this.$refs["addForm"].resetFields();
      }
      this.fetch(objectId);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            axios.post(urls.save, this.form).then(res => {
              this.$message(res.data.result);
              this.dialogVisible = false;
              this.form.value = ''
              this.$emit("refresh");
            });  
        }
        return false;
      });
    },
    fetch(objectId) {
      axios.get(urls.get, { params: { objectId: objectId } }).then(res => {
        let data = res.data.result;
        console.log(data)
        // this.super_depart = {
        //   value: data.value,
        //   objectId : objectId,
        //   note : data.note
        // };
      });
    }
  }
};
</script>